import { UserInfoType } from "@/store/user";
import { ALI_OSS_PATH } from "@/config/path";

const fullImagePath = (path: string) => {
	return `${ALI_OSS_PATH}${path}`;
};

const manageCoverimage = (list: any[], userInfo: UserInfoType) => {
	let languageKey = "cn";
	// 安全解构，确保 userInfo 和 userInfo.points 存在
	let free_times = 0;
	let is_pro = false;
	if (userInfo && userInfo.points) {
		free_times = userInfo.points.free_times || 0; // 如果 free_times 不存在则默认为 0
		is_pro = userInfo.is_pro || false; // 如果 is_pro 不存在则默认为 false
	}
    
	list.forEach((item) => {
		const { charge_type } = item;
		let P = "";
		switch (item.type) {
			case "styler":
				P = "t";
				item.json_content.cover_image = fullImagePath(
					item.json_content.cover_image
				);
				break;
			case "scene":
				P = "t";
				item.json_content.cover_image = fullImagePath(
					item.json_content.cover_image
				);
				break;
			case "sticker":
				P = "t";
				item.json_content.cover_image_list.forEach((cover: any) => {
					cover.path = fullImagePath(cover.cover_image);
				});
				break;
			case "face_swap":
				P = "t";
				item.tpl_image_list.forEach(
					(cover: any) =>
						(cover.cat_s_image = fullImagePath(cover.cat_s_image))
				);
				break;
		}
		if (free_times > 0) {
			if (is_pro) {
				item.iconPath =
					charge_type === "Free"
						? `${ALI_OSS_PATH}/static/index/free_${P}_${languageKey}.png`
						: `${ALI_OSS_PATH}/static/index/vip_free_${P}_${languageKey}.png`;
				item.iconDesc = `剩余${free_times}次`;
			} else {
				item.iconPath =
					charge_type === "Free"
						? `${ALI_OSS_PATH}/static/index/free_${P}_${languageKey}.png`
						: `${ALI_OSS_PATH}/static/index/vip_${P}_${languageKey}.png`;
				item.iconDesc = `剩余${free_times}次`;
			}
		} else {
			if (is_pro) {
				item.iconPath = `${ALI_OSS_PATH}/static/index/point_count_${P}_${languageKey}.png`;
				item.iconDesc = ``;
			} else {
				item.iconPath =
					charge_type === "Free"
						? `${ALI_OSS_PATH}/static/index/point_count_${P}_${languageKey}.png`
						: `${ALI_OSS_PATH}/static/index/vip_${P}_${languageKey}.png`;
				item.iconDesc = charge_type === "Free" ? `` : "会员专属";
			}
		}
	});
	return list;
};

const manmageDecorate = (array: any) => {
	let languagekey = "cn";
	array.forEach((decorateItem: any) => {
		let map1 = new Map();
		let map2 = new Map();
		let showData: any = [];
		for (const key in decorateItem.json_data[languagekey].decorate) {
			if (
				decorateItem.json_data[languagekey].decorate.hasOwnProperty(key)
			) {
				map1.set(
					key,
					decorateItem.json_data[languagekey].decorate[key]
				);
			}
		}
		for (const key in decorateItem.json_data.decorate) {
			if (decorateItem.json_data.decorate.hasOwnProperty(key)) {
				map2.set(key, decorateItem.json_data.decorate[key]);
			}
		}
		for (const key in decorateItem.json_data[languagekey].decorate_name) {
			let name = decorateItem.json_data[languagekey].decorate_name[key];
			let list = map1.get(key);
			let dataList = map2.get(key);
			showData = [
				...showData,
				{ name, option: { list, dataList }, selectVal: 0 },
			];
		}
		decorateItem.json_data[languagekey].showData = showData;
	});
	return array;
};

const manageColor = (object: any) => {
	let data = [];
	data = Object.keys(object).map((item) => {
		let color;
		switch (item) {
			case "red":
				color = "#FF4343";
				break;
			case "orange":
				color = "#FFB443";
				break;
			case "yellow":
				color = "#FFEC43";
				break;
			case "blue":
				color = "#A1FF43";
				break;
			case "purple":
				color = "#43FFC7";
				break;
			case "cyan":
				color = "#43C7FF";
				break;
			case "green":
				color = "#A143FF";
				break;
		}
		return {
			item,
			color,
		};
	});
	return data;
};

export { manageCoverimage, fullImagePath, manmageDecorate, manageColor };
